const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const Task = sequelize.define('Task', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  title: {
    type: DataTypes.STRING,
    allowNull: false
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: false
  },
  type: {
    type: DataTypes.ENUM('daily', 'story', 'training', 'interaction'),
    allowNull: false
  },
  difficulty: {
    type: DataTypes.ENUM('easy', 'medium', 'hard'),
    allowNull: false
  },
  reward: {
    type: DataTypes.JSON,
    allowNull: false,
    defaultValue: { experience: 0 }
  },
  requirements: {
    type: DataTypes.JSON,
    allowNull: true
  },
  storyText: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  choices: {
    type: DataTypes.JSON,
    allowNull: true
  },
  startDate: {
    type: DataTypes.DATE,
    allowNull: true
  },
  endDate: {
    type: DataTypes.DATE,
    allowNull: true
  },
  isActive: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  }
}, {
  timestamps: true
});

// 定义关联关系
Task.associate = (models) => {
  Task.hasMany(models.UserTask, {
    foreignKey: 'taskId',
    as: 'userTasks'
  });
};

module.exports = Task;